﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Report
{
    public class PotentialDangerModel
    {
        public List<PotentialDangersInfo> DangerInfo
        {
            get
            {
                return dangerInfo;
            }
        }

        private List<PotentialDangersInfo> dangerInfo;
        riskanalysisEntities entities = new riskanalysisEntities();

        public void SearchDanger()
        {
            Utils.GetFrequencyList();
            Utils.GetAffectedList();
            Utils.GetControlExistenceList();
            Utils.GetSeverityList();

            IQueryable<danger> query = from d in entities.dangers
                                       select d;
            List<danger> theList = new List<danger>();

            foreach (danger item in query)
            {
                theList.Add(item);
            }

            Utils.GetKValuesList();
            dangerInfo = new List<PotentialDangersInfo>();

            foreach (danger d in theList)
            {
                var theDanger = new
                {
                    DangerId = d.dangerId,
                    Management = d.activity.area.process.management.name,
                    Process = d.activity.area.process.name,
                    Area = d.activity.area.name,
                    Activity = d.activity.name,
                    Causes = d.causes,
                    Consecuences = d.consecuences,
                    Controls = d.controlExistence,
                    Danger = d.description,
                    EspecificUbication = d.ubication,
                    ExistingControls = d.controls,
                    ExpirationDate = d.expirationDate,
                    ExposedNumber = d.exposed,
                    Frequence = d.frecuence,
                    KFactor = d.kFactor,
                    OperativeCondition = d.operativeCondition,
                    PosibleControls = d.posibleControls,
                    Risk = d.risk.name,
                    Severity = d.severity,
                    Status = d.status,
                    AffectedTarget = d.affectedTarget
                };
                PotentialDangersInfo info = new PotentialDangersInfo
                {
                    DangerId = theDanger.DangerId,
                    Management = theDanger.Management,
                    Process = theDanger.Process,
                    Area = theDanger.Area,
                    Activity = theDanger.Activity,                    
                    Controls = Utils.ControlExistance[theDanger.Controls - 1],
                    Danger = theDanger.Danger.Replace("\n", "<br />"),                    
                    ExpirationDate = theDanger.ExpirationDate.ToString("d", Utils.culture),
                    ExposedNumber = Utils.Affecteds[theDanger.ExposedNumber - 1],
                    Frequence = Utils.Frequences[theDanger.Frequence - 1],
                    KFactor = Utils.KValuesDictionary[theDanger.KFactor],                   
                    PosibleControls = theDanger.PosibleControls.Replace("\n", "<br />"),
                    Risk = theDanger.Risk,
                    Severity = Utils.Severitys[theDanger.Severity - 1],
                    Status = theDanger.Status.ToString()
                };
                info.CalculateRisk(d);
                dangerInfo.Add(info);
            }
        }
    }
}